package com.android.lovingpet.util;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Spinner;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.android.lovingpet.Application;
import com.android.lovingpet.R;
import com.android.lovingpet.common.Back;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by ikata on 2018/5/1.
 */
public class LocationClient {

    public static void regeo(HashMap<String,String> params,Back back)
    {
        params.put("key",Application.getContext().getString(R.string.gaodeWebApi));
        MyHttp.getInstance().postAsync(null, URLStore.ADDRESS_REGEO, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.onFailure(MyHttp.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json=response.body().string();
                Log.d("json",json);
                try {
                    JSONObject jsonObject=new JSONObject(json);
                    String status= null;
                    status = jsonObject.getString("status");
                    if (status.equals("1"))
                    {
                        String regeocode=jsonObject.getString("regeocode");
                        jsonObject=new JSONObject(regeocode);
                        String addressComponent=jsonObject.getString("addressComponent");
                        jsonObject=new JSONObject(addressComponent);
                        String province=jsonObject.getString("province");
                        String district=jsonObject.getString("district");
                        String address=province+district;
                        Bundle bundle1=new Bundle();
                        bundle1.putString("address",address);
                        back.onSuccess(bundle1);
                    }
                    else
                    {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public static void getLocation(@Nullable Back back)
    {
        AMapLocationClient locationClientSingle =new AMapLocationClient(Application.getContext());
        AMapLocationListener locationListener= aMapLocation -> {
            if (aMapLocation!=null)
            {
                if (aMapLocation.getErrorCode()==0)
                {
                    Bundle bundle=new Bundle();
                    bundle.putParcelable("result",aMapLocation);

                    String city=aMapLocation.getCity();
                    city=city.substring(0,city.length()-1);
                    //保存位置信息
                    SpUtil.setLocation(aMapLocation.getLongitude(),aMapLocation.getLatitude(),city);

                    if (back!=null)
                    back.onSuccess(bundle);
                }
                else {
                    if (back!=null)
                    back.onFailure("AmapError:location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo());
                }
            }
        };
        locationClientSingle.setLocationListener(locationListener);

        AMapLocationClientOption locationClientSingleOption=new AMapLocationClientOption();
        //获取一次定位结果：
        locationClientSingleOption.setNeedAddress(true);
        //该方法默认为false。
        locationClientSingleOption.setOnceLocation(true);
        //关闭缓存机制
        locationClientSingleOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        locationClientSingle.setLocationOption(locationClientSingleOption);
        //启动定位
        locationClientSingle.startLocation();
    }
}
